import 'dart:io';

import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/page/supplier/model/certificate_entity.dart';
import 'package:tender_flutter/router/router.dart';
import 'package:tender_flutter/style/font_style.dart';
import 'package:tender_flutter/utils/img_picker.dart';
import 'package:tender_flutter/utils/picker.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';
import 'package:tender_flutter/utils/toast.dart';
import 'package:tender_flutter/utils/utils.dart';
import 'package:tender_flutter/widgets/base_appbar.dart';
import 'package:tender_flutter/widgets/form_widget/form_option_item.dart';
import 'package:tender_flutter/widgets/form_widget/form_picture_item.dart';

class SupplierAddCertificatePage extends StatefulWidget {
  /// 资质信息
  final CertificateEntity certificateEntity;

  const SupplierAddCertificatePage({Key key, this.certificateEntity})
      : super(key: key);

  @override
  _SupplierAddCertificatePageState createState() =>
      _SupplierAddCertificatePageState();
}

class _SupplierAddCertificatePageState
    extends State<SupplierAddCertificatePage> {
  GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

  void _preparedSubmit() {
    if (isEmpty(widget.certificateEntity.title)) {
      ToastUtils.show("请输入证书名称");
      return;
    }
    if (isEmpty(widget.certificateEntity.code)) {
      ToastUtils.show("请输入证书编号");
      return;
    }
    if (isEmpty(widget.certificateEntity.regType)) {
      ToastUtils.show("请输入注册类型");
      return;
    }
    if (isEmpty(widget.certificateEntity.regLevel)) {
      ToastUtils.show("请输入注册等级");
      return;
    }
    if (isEmpty(widget.certificateEntity.issueAuthority)) {
      ToastUtils.show("请输入发证机关");
      return;
    }
    if (isEmpty(widget.certificateEntity.issueDate)) {
      ToastUtils.show("请输入发证日期");
      return;
    }
    if (isEmpty(widget.certificateEntity.endDate)) {
      ToastUtils.show("请输入截止日期");
      return;
    }
    if (isEmpty(widget.certificateEntity.licensePic)) {
      ToastUtils.show("请选择证书图片");
      return;
    }

    _forSubmitted();
  }

  void _forSubmitted() async {
    // var _form = _formKey.currentState;
    // if (_form.validate()) {
    //   _form.save();
    //   // print(_username);
    //   // print(_password);
    // }

    LRouter.navigatorCtx(context).pop(widget.certificateEntity);
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: ThemeColors.colorWhite,
        appBar: _initAppBar(),
        body: _initBody());
  }

  /// 导航
  Widget _initAppBar() {
    return BaseAppBar(context, Text("加入供应商", style: FonStyle.navTextStyle()));
  }

  /// 内容体
  Widget _initBody() {
    return SingleChildScrollView(
        child: Form(
            key: _formKey,
            child: Column(children: <Widget>[
              FormOptionItem(
                  title: "证书名称",
                  placeholder: "请输入证书名称",
                  content: widget.certificateEntity.title ?? "",
                  inputOnly: true,
                  onChangeCallback: (value) {
                    widget.certificateEntity.title = value;
                  }),
              FormOptionItem(
                  title: "证书编号",
                  placeholder: "请输入证书编号",
                  content: widget.certificateEntity.code ?? "",
                  inputOnly: true,
                  onChangeCallback: (value) {
                    widget.certificateEntity.code = value;
                  }),
              FormOptionItem(
                  title: "注册类型",
                  placeholder: "请输入注册类型",
                  content: widget.certificateEntity.regType ?? "",
                  inputOnly: true,
                  onChangeCallback: (value) {
                    widget.certificateEntity.regType = value;
                  }),
              FormOptionItem(
                  title: "注册等级",
                  placeholder: "请输入注册等级",
                  content: widget.certificateEntity.regLevel,
                  inputOnly: true,
                  onChangeCallback: (value) {
                    widget.certificateEntity.regLevel = value;
                  }),
              FormOptionItem(
                  title: "发证机关",
                  placeholder: "请输入发证机关",
                  content: widget.certificateEntity.issueAuthority ?? "",
                  inputOnly: true,
                  onChangeCallback: (value) {
                    widget.certificateEntity.issueAuthority = value;
                  }),
              FormOptionItem(
                  title: "发证日期",
                  placeholder: "请选择发证日期",
                  content: widget.certificateEntity.issueDate ?? "",
                  selectCallback: () {
                    PickerUtils.showDatePicker(context,
                        clickCallback: (selectDateStr, selectDate) {
                      setState(() {
                        widget.certificateEntity.issueDate =
                            formatDate(selectDate, [
                          yyyy,
                          '-',
                          mm,
                          '-',
                          dd,
                          " ",
                          "00",
                          ":",
                          "00",
                          ":",
                          "00",
                        ]);
                      });
                    });
                  }),
              FormOptionItem(
                  title: "截止日期",
                  placeholder: "请选择发证日期",
                  content: widget.certificateEntity.endDate ?? "",
                  selectCallback: () {
                    PickerUtils.showDatePicker(context,
                        clickCallback: (selectDateStr, selectDate) {
                      setState(() {
                        widget.certificateEntity.endDate = formatDate(
                            selectDate, [
                          yyyy,
                          '-',
                          mm,
                          '-',
                          dd,
                          " ",
                          "00",
                          ":",
                          "00",
                          ":",
                          "00"
                        ]);
                      });
                    });
                  }),
              FormPictureItem(
                  title: "证书照片",
                  imgFile: widget.certificateEntity.licensePic == null
                      ? null
                      : File(widget.certificateEntity.licensePic),
                  callback: () {
                    ImagePickerUtils.getImage(PICKER_PHOTO).then((value) {
                      if (value != null) {
                        setState(() {
                          widget.certificateEntity.licensePic = value.path;
                        });
                      }
                    });
                  }),
              _getSubmitBtn()
            ])));
  }

  /// 提交按钮
  Widget _getSubmitBtn() {
    return Container(
        height: ScreenAdapter.height(90),
        width: double.infinity,
        margin: EdgeInsets.symmetric(
            vertical: ScreenAdapter.height(50),
            horizontal: ScreenAdapter.width(30)),
        decoration: BoxDecoration(
            color: ThemeColors.colorType6,
            borderRadius: BorderRadius.circular(ScreenAdapter.width(14))),
        child: FlatButton(
            onPressed: () {
              _preparedSubmit();
            },
            child: Text("确定",
                style: TextStyle(
                    color: ThemeColors.colorWhite,
                    fontSize: ScreenAdapter.sp(32)))));
  }
}
